package com.lxm.enigma.filter;

import com.lxm.enigma.algorithm.AesAlgorithm;
import com.lxm.enigma.algorithm.Md5Algorithm;
import com.lxm.enigma.annotations.CipherSign;
import com.lxm.enigma.core.EnigmaConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Objects;

/**
 * @Author: Lys
 * @Date 2022/5/31
 * @Describe
 **/
@Slf4j
@RestControllerAdvice//(basePackages = "com.lxm")
public class EnigmaRequestAdvice implements RequestBodyAdvice {

    private final EnigmaConfig enigmaConfig;
    private final AesAlgorithm aesAlgorithm;
    private final Md5Algorithm md5Algorithm;

    @Autowired
    public EnigmaRequestAdvice(EnigmaConfig enigmaConfig, AesAlgorithm aesAlgorithm, Md5Algorithm md5Algorithm) {
        this.enigmaConfig = enigmaConfig;
        this.aesAlgorithm = aesAlgorithm;
        this.md5Algorithm = md5Algorithm;
    }

    /**
     * 只处理cipherSign类型和post请求
     *
     * @param methodParameter
     * @param targetType
     * @param converterType
     * @return
     */
    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return methodParameter.hasMethodAnnotation(CipherSign.class) && methodParameter.hasMethodAnnotation(PostMapping.class);
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        return inputMessage;
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        var annotation = parameter.getMethodAnnotation(CipherSign.class);
        if (Objects.nonNull(annotation)) {
            String sign = inputMessage.getHeaders().getFirst(enigmaConfig.getSignTag());
            if (StringUtils.isNotBlank(sign)) {
                try {
                    String checkSign = this.md5Algorithm.encipher(body, enigmaConfig.getKey());
                    if (StringUtils.equals(sign, checkSign)) {
                        return body;
                    } else {
                        return null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return body;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return null;
    }

}
